package ar.Location.Helper;

import java.text.DecimalFormat;
import java.util.Random;

import com.google.android.maps.GeoPoint;

import ar.Location.Constant.Define;

public class StringHelper {
	
	public static String getTruncateString(String str) {
		if(str != null) {
			String whiteSpace = " ";
			String[] arrWords = str.split(whiteSpace);
			int nWords = arrWords.length;
			if (nWords > 40) {
				Random rand = new Random();
				int minWords = 20;
				int maxWords = 30;
				int newWords = minWords + rand.nextInt(maxWords - minWords);
				String truncateSymbol = "...";
				String strResult = "";
				for (int i = 0; i < newWords - 1; i++) {
					strResult += arrWords[i] + whiteSpace;
				}
				strResult += arrWords[newWords - 1] + truncateSymbol;
				return strResult;
			}
		}
		return str;
	}

	public static boolean checkIfCorrectDistanceFormat(String str) {
		char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
				'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
				'x', 'y', 'z' };
		
		int size = chars.length;
		String strCheck = str.toLowerCase();
		
		for(int i = 0; i < size; i ++) {
			if(strCheck.contains(chars[i] + "")) {
				return false;
			}
		}
		
		return true;
	}

	public static String getStringDistance(int meters) {
		if (meters < Define.KM) {
			return meters + " m";
		} else {
			float kilometers = (float) ((meters * 1.0) / Define.KM);
			DecimalFormat df = new DecimalFormat("#.##");
			return df.format(kilometers) + " km";
		}
	}

	public static String edit(String imageUrl) {
		// TODO Auto-generated method stub
		if(imageUrl != null && imageUrl.length() > 0) {
			imageUrl = imageUrl.replace(".png", "_256.png");
			return imageUrl;
		}
		return null;
	}
	
	public static String getStringLatitude(GeoPoint geoPoint) {
		if(geoPoint != null) {
			int lat = geoPoint.getLatitudeE6();
			float latitude = (float)((lat * 1.0) / 1E6);
			return String.valueOf(latitude);
		}
		return null;
	}
	
	public static String getStringLongitude(GeoPoint geoPoint) {
		if(geoPoint != null) {
			int lng = geoPoint.getLongitudeE6();
			float longitude = (float)((lng * 1.0) / 1E6);
			return String.valueOf(longitude);
		}
		return null;
	}
}
